package org.jeecg.modules.bill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.bill.entity.MedicineLoss;
import org.jeecg.modules.bill.mapper.MedicineLossMapper;
import org.jeecg.modules.bill.service.MedicineLossService;
import org.jeecg.modules.stock.entity.MedicineStock;
import org.jeecg.modules.stock.mapper.MedicineStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 药品报损单表
 * @Author: 孔小波
 * @Date:   2020-03-24
 * @Version: V1.0
 */
@Service
public class MedicineLossServiceImpl extends ServiceImpl<MedicineLossMapper, MedicineLoss> implements MedicineLossService {

    @Autowired
    private MedicineLossMapper medicineLossMapper;
    @Autowired
    private MedicineStockMapper medicineStockMapper;

    @Override
    @Transactional
    public void saveMedicineLoss(MedicineLoss medicineLoss) {
        //设置报损账单状态为0未审核
        medicineLoss.setStatus("0");
        medicineLossMapper.insert(medicineLoss);
        //设置该条库存数据为报损状态0
        MedicineStock medicineStock=medicineStockMapper.selectById(medicineLoss.getSid());
        medicineStock.setStatus("0");//报损状态
        medicineStockMapper.updateById(medicineStock);
    }
    //审核通过后才删除库存数据
    @Override
    @Transactional
    public void examineMedicineLoss(MedicineLoss medicineLoss) {
        //设置状态为1通过审核
        if(medicineLoss.getStatus().equals("0")){ //审核不通过
            medicineLossMapper.deleteById(medicineLoss.getBid());
            //更新库存数据，将该条库存数据变成1正常状态
            MedicineStock medicineStock=medicineStockMapper.selectById(medicineLoss.getSid());
            medicineStock.setStatus("1");//正常状态
            medicineStockMapper.updateById(medicineStock);
        }else {
            medicineLossMapper.updateById(medicineLoss);
            medicineStockMapper.deleteById(medicineLoss.getSid());
        }
    }

}
